package by.demchenko.sample.webapp.page.home.share;

import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.services.UserService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.BaseLayout;
import by.demchenko.sample.webapp.page.home.share.CoursesPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;

import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RegistrationPage extends BaseLayout {

    @Inject
    UserService userService;


    public RegistrationPage(Course course, Date dateOfCourse) {
        add(new Label("course", course.getName()));
        add(new Label("teacher", course.getTeacher().getFullName()));
        SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
        add(new Label("date", DATE_FORMAT.format(dateOfCourse)));

        add(new Link("yesButton") {
            @Override
            public void onClick() {
                if (!userService.enrollInCourse(BasicAuthenticationSession.get().getUser(), course, dateOfCourse)) {
                    error("Ошибка при регистрации!");
                } else {
                    setResponsePage(new CoursesPage());
                }
            }
        });

        add(new Link("noButton") {
            @Override
            public void onClick() {
                setResponsePage(new CoursesPage());
            }
        });
    }

    public RegistrationPage() {
        super();

    }
}
